<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------

namespace app\api\controller;


use app\api\lists\OrdersLists;
use app\api\lists\OrderstartLists;
use app\api\logic\OrderLogic;
use app\api\validate\OrderValidate;
use app\api\validate\PlaceOrdersValidate;
use app\common\enum\OrderEnum;
use app\common\model\goods\Goods;
use app\common\model\order\Order;
use app\common\model\order\OrderGoods;
use app\common\model\order\Orders;
use app\common\model\user\User;

class OrdersController extends BaseShopController
{
    /**
     * @notes 提交订单
     * @return \think\response\Json
     * @author ljj
     * @date 2022/2/25 10:12 上午
     */
    public function placeOrder()
    {
        $params= $this->request->post();
        $params['user_id']=$this->userId;
        // 获取当前日期的年月日时分
        $order = [
            'sn'                    => generate_sn((new Order()), 'sn'),
            'user_id'               => $params['user_id'],
            'clinic_id'   =>$params['clinic_id'],
            'order_remarks'=>$params['order_remarks'],
            'province_id'           => $params['province_id'],
            'city_id'               => $params['city_id'],
            'district_id'           => $params['district_id'],
            'address'               => $params['address'],
            'appoint_time_start'    => $params['appoint_time_start'],
            'appoint_time_end'      => $params['appoint_time_end'],
            'doctor_name'=>$params['doctor_name'],
            'channel'=>$params['channel'],
            "goods_id"=>$params['goods_id']
        ];
       $model= new Orders();
       $data=$model->save($order);
      if ($data){
          return $this->success("",['data'=>1],1,1);
      }

        return $this->success("",['data'=>2],1,1);
    }
    public function Orderup(){
      $id=  $this->request->get("id");
       $model= new Orders();
        $model->where("id","=",$id)->update(["order_status"=>1]);
    }
    public function cancellation(){
        $id=  $this->request->get("id");
        $model= new Orders();
        $model->where("id","=",$id)->update(["order_status"=>2]);
        return $this->success("",['data'=>1],1,1);
    }
    public function goods(){
        $model= new Goods();
        return $this->success("",['data'=>$model->select()->toArray()],1,1);
    }
    /**
     * @notes 订单列表
     * @return \think\response\Json
     * @author ljj
     * @date 2022/2/28 10:01 上午
     */
    public function lists()
    {
        return $this->dataLists(new OrdersLists());
    }

    public function liststart(){
        $params = $this->request->get();
        $orderQuery = new Orders();
        $user_id=$this->userId;
        $orderQuery = $orderQuery->where("detailed", "=", $params['detailed']);

        if (!empty($params['start_time']) && $params['start_time']!="") {
            $startTime = (int) ($params['start_time'] / 1000);
            $orderQuery = $orderQuery->where('create_time', '>=', $startTime);
        }

        if (!empty($params['end_time']) && $params['end_time']!="") {
            $endTime = (int) ($params['end_time'] / 1000);
            $orderQuery = $orderQuery->where('create_time', '<=', $endTime);
        }

        $list= $orderQuery
            ->with(['goods','zhensuo'])
             ->where("user_id","=",$user_id)
            ->where("order_status","<>",2)
            ->append(['zhensuoname',"Province","City","District","doctornum"])
            ->select()

            ->toArray();

        return $this->success('', ['data' => $list], 1, 1);
    }
    /**
     * @notes 订单详情
     * @return \think\response\Json
     * @author ljj
     * @date 2022/2/28 11:23 上午
     */
    public function detail()
    {
        $params =$this->request->get();
        $result =new  Orders();
       $list= $result->where("id","=",$params['id'])->with(['goods','zhensuo'])
           ->append(['zhensuoname',"Province","City","District"])->findOrEmpty()->toArray();
        return $this->success('',$list,1,1);
    }

    /**
     * @notes 取消订单
     * @return \think\response\Json
     * @author ljj
     * @date 2022/2/28 11:36 上午
     */
    public function cancels()
    {
        $params = (new OrderValidate())->post()->goCheck('cancel');
        $params['user_id'] = $this->userId;
        $result = (new OrderLogic())->cancel($params);
        if (true !== $result) {
            return $this->fail($result);
        }
        return $this->success('操作成功',[],1,1);
    }

    /**
     * @notes 删除订单
     * @return \think\response\Json
     * @author ljj
     * @date 2022/2/28 11:50 上午
     */
    public function del()
    {
        $params = (new OrderValidate())->post()->goCheck('del');
        (new OrderLogic())->del($params['id']);
        return $this->success('操作成功',[],1,1);
    }
}